import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { DictDataApi } from '#/api/system/dict/dictdata';
import { gridDictType } from '#/api/system/dict/dicttype';
import { $t } from '#/locales';

export function getDictTypeOptions() {
  return [
    {
      color: 'processing',
      label: $t('system.menu.typeCatalog'),
      value: 'catalog',
    },
    { color: 'default', label: $t('system.menu.typeMenu'), value: 'menu' },
    { color: 'error', label: $t('system.menu.typeButton'), value: 'button' },
    {
      color: 'success',
      label: $t('system.menu.typeEmbedded'),
      value: 'embedded',
    },
    { color: 'warning', label: $t('system.menu.typeLink'), value: 'link' },
  ];
}

export function dictFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'dictCode',
      formItemClass: 'col-span-2 md:col-span-2 pb-0',
      label: $t('system.dict.dictData.dictCode'),
      rules: 'required',
    },
    {
      component: 'Input',
      fieldName: 'dictName',
      formItemClass: 'col-span-2 md:col-span-2 pb-0',
      label: $t('system.dict.dictData.dictName'),
      rules: 'required',
    },
    {
      component: 'ApiTreeSelect',
      componentProps: {
        allowClear: true,
        api: gridDictType,
        class: 'w-full',
        labelField: 'dictTypeName',
        valueField: 'dictTypeCode',
        childrenField: 'children',
      },
      fieldName: 'dictTypeCode',
      label: $t('system.dict.dictData.dictType'),
    },
    {
      component: 'Input',
      fieldName: 'table',
      label: $t('system.dict.dictData.tableName'),
    },
    {
      component: 'Input',
      fieldName: 'seq',
      label: $t('system.dict.dictData.seq'),
    },
    {
      component: 'RadioGroup',
      componentProps: {
        buttonStyle: 'solid',
        options: [
          { label: $t('common.enabled'), value: 1 },
          { label: $t('common.disabled'), value: 0 },
        ],
        optionType: 'button',
      },
      defaultValue: 1,
      fieldName: 'status',
      label: $t('system.dict.dictData.status'),
    },
    {
      component: 'Textarea',
      fieldName: 'remark',
      label: $t('system.role.remark'),
    },
  ];
}
export function dictColumns(
  onActionClick: OnActionClickFn<DictDataApi.DictData>,
  onStatusChange?: (newStatus: any, row: T) => PromiseLike<boolean | undefined>,
): VxeTableGridOptions<DictDataApi.DictData>['columns'] {
  return [
    {
      field: 'dictCode',
      title: $t('system.dict.dictData.dictCode'),
      width: 100,
    },
    {
      align: 'center',
      field: 'dictName',
      title: $t('system.dict.dictData.dictName'),
      width: 200,
    },
    {
      align: 'center',
      field: 'dictTypeName',
      title: $t('system.dict.dictData.dictTypeName'),
      width: 150,
    },
    {
      align: 'center',
      field: 'seq',
      title: $t('system.dict.dictData.seq'),
      width: 80,
    },
    {
      cellRender: {
        attrs: { beforeChange: onStatusChange },
        name: onStatusChange ? 'CellSwitch' : 'CellTag',
      },
      field: 'status',
      title: $t('system.menu.status'),
      width: 100,
    },
    {
      field: 'memo',
      title: $t('system.dict.dictData.memo'),
      width: 150,
    },
    {
      align: 'right',
      cellRender: {
        attrs: {
          nameField: 'dictName',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          'edit', // 默认的编辑按钮
          'delete', // 默认的删除按钮
        ],
      },
      field: 'operation',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      title: $t('system.menu.operation'),
      width: 150,
    },
  ];
}
